#!/bin/zsh

GREEN_START="\e[32m"
GREEN_STOP="\e[0m"
RED_START="\e[31m"
RED_STOP="\e[0m"

PHPLDAPADMIN_LOG_FILE=/var/log/phpldapadmin.log

log () {
	echo $1 | /usr/bin/tee -a ${PHPLDAPADMIN_LOG_FILE}
}

get_pid () {
	PATTERN=$1
	/bin/ps aux|/bin/grep "${PATTERN}" | /bin/sed '/grep/d'| /usr/bin/awk '{print $1}'
}

ok () {
	echo "[ "${GREEN_START}"OK"${GREEN_STOP}" ]"
}

failed () {
	echo "[ "${RED_START}"FAILED"${RED_STOP}" ]"
}

generate_phpldapadmin_nginx_conf () {
	/bin/cat << EOF >/etc/nginx/http.d/phpldapadmin.conf 
# vim: ft=nginx
server {
	server_name openldap.domain;
	listen 80 default_server;
	listen [::]:80 default_server;
	# listen 443 ssl;
	# listen [::]:443 ssl;
	root /usr/share/webapps/phpldapadmin/htdocs;
	location / {
		index index.php index.html index.htm;
		charset utf-8;
	}
	# You may need this to prevent return 404 recursion.
	location = /404.html {
		internal;
	}
	# PHP 脚本请求全部转发到 FastCGI处理. 使用FastCGI协议默认配置.
	# Fastcgi服务器和程序(PHP,Python)沟通的协议.
	location ~ \.php$ {
		# 设置监听端口
		fastcgi_pass   127.0.0.1:9000;
		# 设置nginx的默认首页文件(上面已经设置过了，可以删除)
		fastcgi_index  index.php;
		# 设置脚本文件请求的路径
		fastcgi_param  SCRIPT_FILENAME  \$document_root\$fastcgi_script_name;
		fastcgi_param  ENV_CAPI_DEBUG true;
		fastcgi_param  APP_ENV development;
		# 引入fastcgi的配置文件
		include        fastcgi_params;
	}
}
EOF
}

generate_phpldapadmin_conf () {
	if [ -f /srv/podman/openldap/phpldapadmin/phpldapadmin_config.php ]; then
		/bin/cp /srv/podman/openldap/phpldapadmin/phpldapadmin_config.php /etc/phpldapadmin/config.php 
	fi
}

install_phpldapadmin () {
	echo -n "installing phpldapadmin: "
	/etc/entrypoint.d/04-php_fpm status > /dev/null || /etc/entrypoint.d/04-php_fpm start > /dev/null
	PHPLDAPADMIN_INSTALLED=`/sbin/apk list phpldapadmin 2>/dev/null`
	if [[ -z ${PHPLDAPADMIN_INSTALLED} ]]; then
		/sbin/apk add phpldapadmin php-ldap php-gettext php-xml 2> /dev/null || failed
	fi
	if [ -f /etc/nginx/http.d/default.conf ]; then
		/bin/rm /etc/nginx/http.d/default.conf
	fi
	if [ ! -f /etc/nginx/http.d/phpldapadmin.conf ]; then
		generate_phpldapadmin_nginx_conf
	fi
	if [ ! -f /etc/phpldapadmin/config.php ]; then
		generate_phpldapadmin_conf
	fi
	ok
}

status_phpldapadmin () {
	echo -n "status of service phpldapadmin: "
	PHPLDAPDMIN_NGINX_CONF=/etc/nginx/http.d/phpldapadmin.conf
	if [ ! -f ${PHPLDAPDMIN_NGINX_CONF} ]; then
		log "[ "${RED_START}"FAILED"${RED_STOP}" ]"
		log "PHPLDAPDMIN_NGINX_CONF file not found: ${PHPLDAPDMIN_NGINX_CONF} "
		return 1
	fi
	PHPLDAPDMIN_PHP_CONF=/etc/phpldapadmin/config.php
	if [ ! -f ${PHPLDAPDMIN_PHP_CONF} ]; then
		log "PHPLDAPDMIN_PHP_CONF file not found: ${PHPLDAPDMIN_PHP_CONF}"
		return 1
	fi
	IS_NGINX_RUNNING=`/bin/ps aux | /bin/grep "nginx: master" | /bin/sed '/grep/d' `
	if [ -z ${IS_NGINX_RUNNING} ]; then
		log "nginx is not running"
		return 1
	fi
	ok
	return 0
}

start_phpldapadmin () {
	echo -n "starting phpldapadmin: "
	install_phpldapadmin > /dev/null \
		&& /srv/entrypoint.d/php-fpm start > /dev/null \
		&& /srv/entrypoint.d/nginx reload > /dev/null \
		&& ok
}

main ()  {
	action=$1
	case $action in
		status)
			status_phpldapadmin
			;;
		start)
			start_phpldapadmin
			;;
		install)
			install_phpldapadmin
			;;
		*)
	esac
}

main $@

